<template>
  <van-cell class="van-field-cell" title-class="van-field__label" :required="required">
    <template #title>
      <span class="">{{ label }}</span>
    </template>
    <template #value>
      <van-checkbox-group v-model="checked" class="checkbox-list" @change="change" shape="square">
        <van-checkbox
          class="checkbox-item"
          v-for="option in options"
          :key="option"
          :name="option.value"
        >
          {{ option.label }}
        </van-checkbox>
      </van-checkbox-group>
    </template>
  </van-cell>
</template>

<script setup lang="ts">
const emits = defineEmits(['update:modelValue', 'change'])
const props = defineProps({
  modelValue: {
    type: Array as () => String[],
    default: () => {
      return []
    }
  },
  label: {
    type: String,
    default: () => {
      return ''
    }
  },
  options: {
    type: Array as () => any[],
    default: () => {
      return []
    }
  },
  required: {
    type: Boolean,
    default: () => {
      return false
    }
  }
})
const checked = ref<string>(props.modelValue)
const change = (name: string) => {
  emits('change', name)
  emits('update:modelValue', name)
}
watch(props, () => {
  if (props.modelValue != checked.value) {
    checked.value = props.modelValue
  }
})
</script>

<style lang="scss" scoped>
.checkbox-list {
  display: flex;
  flex-wrap: wrap;
  --van-checkbox-checked-icon-color: var(--el-color-primary);
  .checkbox-item {
    margin-right: 20px;
  }
}
.van-field-cell {
  margin-left: 6px;
  &.van-cell--required:before {
    left: 10px;
  }
}
</style>
